package haizhi.fruitmall.util;

import org.springframework.beans.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.text.NumberFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * Created by richa on 2017/8/21
 */
public class BeanUtil {

    public static final String NUMBER_CHAR = "123456789";
    public static final String imagePath = "/static/image";

    public static String getPath(HttpServletRequest request){
        return request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+imagePath;
    }

    public static boolean isNull(Object object) {
        return object == null;
    }

    public static Boolean noNull(Object object){
        return object != null;
    }

    public static Boolean isEmpty(String string) {
        return string == null || string.trim().isEmpty();
    }

    public static Boolean noEmpty(String string) {
        return string != null && !string.trim().isEmpty();
    }

    public static Boolean isEmpty(List list){
        return list == null || list.isEmpty();
    }

    public static Boolean noEmpty(List list){
        return list != null && !list.isEmpty();
    }


    //生成唯一主键
    public static synchronized String genKey(){
        Random random = new Random();
        Integer integer = random.nextInt(900000)+100000;
        return System.currentTimeMillis()+String.valueOf(integer);
    }

    //拷贝属性
    public static void copyBean(Object source, Object target){
        BeanUtils.copyProperties(source,target);
    }

    //匹配模糊
    public static String isLike(String string) { return "%" + checkCode(string) + "%"; }

    //生成创建时间
    public static Date newTime() {
        return new Date();
    }

    //输出百分数
    public static String toPercent(BigDecimal decimal, Integer digit){
        NumberFormat numberFormat = NumberFormat.getPercentInstance();
        numberFormat.setMinimumFractionDigits(digit);
        return numberFormat.format(decimal);
    }

    //生成九位整型编号
    public static synchronized Integer intId() {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < 9; i++) {
            sb.append(NUMBER_CHAR.charAt(random.nextInt(NUMBER_CHAR.length())));
        }
        return new Integer(sb.toString());
    }

    //生成十五位长整型编号
    public static synchronized Long longId() {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < 15; i++) {
            sb.append(NUMBER_CHAR.charAt(random.nextInt(NUMBER_CHAR.length())));
        }
        return new Long(sb.toString());
    }

    //解决中文乱码
    public static String checkCode(String string) {
        try {
            if (!Charset.forName("GBK").newEncoder().canEncode(string)) {
                return new String(string.getBytes("ISO-8859-1"), "UTF-8");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return string;
    }

    //文件路径
    public static String newFileName(String fileName) {
        Long uploadTime = System.currentTimeMillis();
        String fileType = fileName.substring(fileName.lastIndexOf('.'), fileName.length());
        return uploadTime + fileType;
    }

}
